package com.njitt.bysj.srevice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.*;
import com.njitt.bysj.mapper.*;
import com.njitt.bysj.query.LinkManQuery;
import com.njitt.bysj.query.MaterialQuery;
import com.njitt.bysj.query.PurchaseRequestQuery;
import com.njitt.bysj.query.SupplierQuery;
import com.njitt.bysj.utils.AssertUtil;
import com.njitt.bysj.utils.PhoneUtil;
import jdk.nashorn.internal.objects.NativeNumber;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.el.MapELResolver;
import java.util.*;

/**
 * @description:
 * @author: wyj
 * @time: 2022/4/13 15:47
 */
@Service
public class SupplierService extends BaseService<SupplierBean, Integer> {

    @Resource
    private SupplierBeanMapper supplierBeanMapper;
    @Resource
    private MaterialBeanMapper materialBeanMapper;
    @Resource
    private EmpService empService;
    @Resource
    private LinkManBeanMapper linkManBeanMapper;
    @Resource
    private SupplierMaterialBeanMapper supplierMaterialBeanMapper;
    @Resource
    private ProjectBeanMapper projectBeanMapper;
    @Resource
    private ProjectPurchaseBeanMapper projectPurchaseBeanMapper;
    @Resource
    private WarehouseBeanMapper warehouseBeanMapper;
    @Resource
    private PurchaseRequestBeanMapper purchaseRequestBeanMapper;


    public Map<String, Object> supplierListByQuery(SupplierQuery supplierQuery) {
        Map<String, Object> map = new HashMap<>(16);
        PageHelper.startPage(supplierQuery.getPage(), supplierQuery.getLimit());
        PageInfo<SupplierBean> info = new PageInfo<>(supplierBeanMapper.selectByParams(supplierQuery));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("count", info.getTotal());
        map.put("msg", "success");
        return map;
    }

    public Map<String, Object> materialListByQuery(MaterialQuery query) {
        Map<String, Object> map = new HashMap<>(16);
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<MaterialBean> info = new PageInfo<>(materialBeanMapper.selectByParams(query));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addMaterial(MaterialBean materialBean) {
        /**
         * @description: 添加物料
         * @return:  * @param materialBean
         * @author:
         * @time: 2022/4/18 12:24
         */
        AssertUtil.isTrue(materialBean.getMaterialEmpid() == null, "添加物料用例失败");
        EmpBean empBean = empService.selectByPrimaryKey(materialBean.getMaterialEmpid());
        AssertUtil.isTrue(empBean == null, "添加物料用例为空");
        AssertUtil.isTrue(StringUtils.isBlank(materialBean.getMaterialName()), "物料名称不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(materialBean.getMaterialUnit()), "物料单位不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(materialBean.getMaterialNo()), "物料编码不能为空");
        MaterialBean material = materialBeanMapper.selectByMaterialNo(materialBean.getMaterialNo());
        AssertUtil.isTrue(material != null, "物料编码不能重复");
        materialBean.setMaterialStatus(0);
        materialBean.setMaterialCreateDate(new Date());
        materialBean.setMaterialUpdateDate(new Date());
        Integer integer = materialBeanMapper.insertHasKey(materialBean);
        // 执行插入库存表 新物料用例默认库存为0
        WarehouseBean warehouseBean = new WarehouseBean();
        AssertUtil.isTrue(materialBean.getId() == null || materialBean.getId() == 0, "插入失败");
        warehouseBean.setMaterialId(materialBean.getId());
        warehouseBean.setMaterialNo(materialBean.getMaterialNo());
        warehouseBean.setMaterialCount(0);
        warehouseBean.setWarehouseDate(new Date());
        warehouseBean.setWarehouseStatus(0);
        //

        Integer integer1 = warehouseBeanMapper.insertSelective(warehouseBean);
        AssertUtil.isTrue(integer1 == null || integer1 == 0, "添加新物料失败");
        AssertUtil.isTrue(integer == null || Integer.parseInt(String.valueOf(integer)) == 0, "添加失败");
    }

    public List<Map<String, Object>> queryMainMaterialInSupplier(Integer supplierId) {
        AssertUtil.isTrue(supplierId == null || supplierId == 0, "查询失败");
        return materialBeanMapper.queryMainMaterialInSupplier(supplierId);
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addSupplier(SupplierBean supplierBean) {
        AssertUtil.isTrue(supplierBean.getSupplierEmpid() == null, "添加失败， 请登录");
        EmpBean empBean = empService.selectByPrimaryKey(supplierBean.getSupplierEmpid());
        AssertUtil.isTrue(empBean == null, "添加失败");
        AssertUtil.isTrue(StringUtils.isBlank(supplierBean.getSupplierAddress()), "供应商地址不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(supplierBean.getSupplierName()), "供应商名称不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(supplierBean.getSupplierLevel()), "供应商等级不能为空");
        supplierBean.setSupplierStatus(0);
        supplierBean.setSupplierCreateDate(new Date());
        supplierBean.setSupplierUpdateDate(new Date());
        Integer integer = supplierBeanMapper.insertSelective(supplierBean);
        AssertUtil.isTrue(integer == null || Integer.parseInt(String.valueOf(integer)) == 0, "添加失败");
    }

    public Map<String, Object> selectLinkManByQuery(LinkManQuery query) {
        AssertUtil.isTrue(query.getSupplierId() == null, "查看失败");
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<LinkManBean> info = new PageInfo<>(linkManBeanMapper.selectByParams(query));
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("count", info.getTotal());
        map.put("msg", "success");
        return map;
    }

    public void addLinkMan(LinkManBean linkManBean) {
        checkLinkMan(linkManBean);
        linkManBean.setLinkmanStatus(0);
        linkManBean.setLinkmanUpdateDate(new Date());
        linkManBean.setLinkmanCreateDate(new Date());
        Integer integer = linkManBeanMapper.insertSelective(linkManBean);
        AssertUtil.isTrue(integer == null || Integer.parseInt(String.valueOf(integer)) == 0, "添加失败");
    }

    public LinkManBean selectLinkManBeanById(Integer id) {
        AssertUtil.isTrue(id == null, "更新联系人失败");
        LinkManBean linkManBean = linkManBeanMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(linkManBean == null, "更新联系人失败");
        return linkManBean;
    }

    public void checkLinkMan(LinkManBean linkManBean) {
        AssertUtil.isTrue(linkManBean.getSupplierId() == null, "供应商ID 不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(linkManBean.getLinkmanJob()), "联系人职位不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(linkManBean.getLinkmanEmail()), "联系人邮箱不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(linkManBean.getLinkmanPhone()), "联系人联系方式不能为空");
        AssertUtil.isTrue(!PhoneUtil.isMobile(linkManBean.getLinkmanPhone()), "联系方式格式不正确");
        AssertUtil.isTrue(linkManBean.getLinkmanSex() == null, "联系人性别不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(linkManBean.getLinkmanName()), "联系人姓名不能为空");
    }

    public void updateLinkMan(LinkManBean linkManBean) {
        checkLinkMan(linkManBean);
        Integer integer = linkManBeanMapper.updateByPrimaryKeySelective(linkManBean);
        AssertUtil.isTrue(integer == null || integer == 0, "更新失败");
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteLinkManByIds(Integer[] ids) {
        AssertUtil.isTrue(ids.length == 0 || ids.length < 1, "待删除记录不存在");
        AssertUtil.isTrue(linkManBeanMapper.deleteBatch(ids) != ids.length, "删除失败");
    }

    public SupplierBean selectSupplierById(Integer id) {
        AssertUtil.isTrue(id == null, "供应商编号不能为空");
        SupplierBean supplierBean = supplierBeanMapper.selectByPrimaryKey(id);
        return supplierBean;
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateSupplier(SupplierBean supplierBean) {
        AssertUtil.isTrue(supplierBean.getId() == null, "供应商id不能为空");
        AssertUtil.isTrue(supplierBean.getSupplierMainCommodity() == null || supplierBean.getSupplierMainCommodity() == 0
                , "主要产品不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(supplierBean.getSupplierName()), "供应厂商姓名不能为空");
        AssertUtil.isTrue(supplierBean.getSupplierMainCommodity() == null, "主要产品不能为空");
        Integer integer = supplierBeanMapper.updateByPrimaryKeySelective(supplierBean);
        AssertUtil.isTrue(integer == null || integer == 0, "更新失败");

    }

    public Map<String, Object> materialListBySupplierQuery(MaterialQuery query) {
        AssertUtil.isTrue(query.getSupplierId() == null, "查看失败");
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<SupplierMaterialBean> info = new PageInfo<>(supplierMaterialBeanMapper.selectByParams(query));
        Map<String, Object> map = new HashMap<>(16);
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("count", info.getTotal());
        map.put("msg", "success");
        return map;
    }

    public List<Map<String, Object>> materialSelected() {
        return materialBeanMapper.queryAllMaterialBySelected();
    }

    public void addMaterialBySupplier(SupplierMaterialBean supplierMaterialBean) {
        AssertUtil.isTrue(supplierMaterialBean == null, "添加失败");
        AssertUtil.isTrue(supplierMaterialBean.getMaterialId() == null, "添加失败");
        AssertUtil.isTrue(supplierMaterialBean.getSupplierId() == null, "添加失败");
        SupplierMaterialBean bean = supplierMaterialBeanMapper.selectBySupplier(supplierMaterialBean);
        AssertUtil.isTrue(bean != null, "不能重复添加");
        Integer integer = supplierMaterialBeanMapper.insertSelective(supplierMaterialBean);

        AssertUtil.isTrue(integer == null, "添加失败");
    }

    public ProjectBean selectProjectByProjectNo(String projectNo) {
        /**
         * @description: 根据项目编号查项目
         * @return:  * @param projectNo
         * @author:
         * @time: 2022/4/18 16:45
         */
        AssertUtil.isTrue(StringUtils.isBlank(projectNo), "项目编号为空");
        ProjectBean projectBean = projectBeanMapper.selectProjectByProjectNo(projectNo);
        return projectBean;
    }

    public List<Map<String, Object>> selectSupplierToSelect() {
        /**
         * @description: 供应商下拉框
         * @return:  * @param
         * @author:
         * @time: 2022/4/18 16:46
         */
        return supplierBeanMapper.selectSupplierToSelect();
    }

    public List<Map<String, Object>> selectMaterialBySupplerIdToAddPurchaseOrder(Integer supplierId) {
        /**
         * @description: 二级联动 显示物料编码
         * @return:  * @param supplerId
         * @author:
         * @time: 2022/4/18 17:03
         */
        AssertUtil.isTrue(supplierId == null || supplierId == 0, "显示失败");
        return materialBeanMapper.selectMaterialBySupplerIdToAddPurchaseOrder(supplierId);
    }

    public String selectMaterialNameByMaterialIdToAddPurchaseOrder(String materialNo) {
        AssertUtil.isTrue(StringUtils.isBlank(materialNo), "显示失败");
        return materialBeanMapper.selectMaterialNameByMaterialIdToAddPurchaseOrder(materialNo);
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void addPurchaseRequestOrder(PurchaseRequestBean purchaseRequestBean) {
        /** 添加采购申请单
         * @description:
         * @return:  * @param purchaseRequestBean
         * @author:
         * @time: 2022/4/19 9:55
         */
        AssertUtil.isTrue(StringUtils.isBlank(purchaseRequestBean.getMaterialNo()), "物料编码不能为空");
        AssertUtil.isTrue(StringUtils.isBlank(purchaseRequestBean.getProjectNo()), "项目编号不能为空");
        //   AssertUtil.isTrue(StringUtils.isBlank(purchaseRequestBean.getMaterialName()), "物料名称不能为空");
        AssertUtil.isTrue(purchaseRequestBean.getOrderType() == null, "订单类型不能为空");
        AssertUtil.isTrue(purchaseRequestBean.getPurchaseCount() <= 0, "数量不合理");
        AssertUtil.isTrue(purchaseRequestBean.getPurchasePrice() == null, "预估金额不能为空");
        AssertUtil.isTrue(purchaseRequestBean.getSupplierId() == null, "供应商不能为空");
        AssertUtil.isTrue(purchaseRequestBean.getProjectId() == null, "项目编号");
        purchaseRequestBean.setOrderStatus(0);
        purchaseRequestBean.setPurchaseCreateDate(new Date());
        purchaseRequestBean.setPurchaseUpdateDate(new Date());
        Integer integer = purchaseRequestBeanMapper.insertSelective(purchaseRequestBean);
        AssertUtil.isTrue(integer == null || integer == 0, "添加失败");
        // 更新采购员订单状态
        ProjectPurchaseBean projectPurchaseBean = new ProjectPurchaseBean();
        projectPurchaseBean.setStatus(2);
        projectPurchaseBean.setProjectId(purchaseRequestBean.getProjectId());
        Integer integer1 = projectPurchaseBeanMapper.updatePurchaseProjectStatus(projectPurchaseBean);
        AssertUtil.isTrue(integer1 == null || integer != 1, "添加失败");
    }

    public Map<String, Object> selectPurchaseRequest(PurchaseRequestQuery requestQuery) {
        Map<String, Object> map = new HashMap<>(16);
        PageHelper.startPage(requestQuery.getPage(), requestQuery.getLimit());
        PageInfo<PurchaseRequestBean> info = new PageInfo<>(purchaseRequestBeanMapper.selectByParams(requestQuery));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("msg", "success");
        map.put("count", info.getTotal());
        return map;
    }


    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void assignPurchaseRequestOrder(List<PurchaseRequestBean> requestBeanList) {
        /**
         * @description: 审批采购申请单
         * @return:  * @param requestBeanList
         * @author:
         * @time: 2022/4/19 14:01
         */
        AssertUtil.isTrue(requestBeanList.size() == 0, "审批失败");
        requestBeanList.forEach(requestPurchaseBean -> {
            requestPurchaseBean.setOrderStatus(1);
            requestPurchaseBean.setPurchaseNo(System.currentTimeMillis() + "");
        });
        Integer integer = purchaseRequestBeanMapper.updateBatch(requestBeanList);
        AssertUtil.isTrue(integer != 1, "审批失败2");
    }

    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    @Transactional(propagation = Propagation.REQUIRED)
    public void updatePurchaseOrderStatus(boolean approve, Integer id) {
        if (approve == true) {
            /**
             * @description: 说明质检同意入库  采购订单结束
             * @return:  * @param approve
             * @author:
             * @time: 2022/4/20 10:14
             */
            PurchaseRequestBean bean = new PurchaseRequestBean();
            bean.setOrderStatus(3);
            bean.setId(id);
            Integer integer = purchaseRequestBeanMapper.updateByPrimaryKeySelective(bean);
            AssertUtil.isTrue(integer != 1, "修改失败");
        }else if (approve == false){
            PurchaseRequestBean bean = new PurchaseRequestBean();
            bean.setOrderStatus(1);
            bean.setId(id);
            Integer integer = purchaseRequestBeanMapper.updateByPrimaryKeySelective(bean);
            AssertUtil.isTrue(integer != 1, "修改失败");
        }
    }
}
